<?php
/**
 * bootstrap.php
 *
 * main bootstrap for Madoqua
 *
 * @category 	Madoqua
 * @package 	Bootstrap
 *
 */
try {

    error_reporting(E_ALL | E_STRICT);
    ini_set('display_errors', 'On');
    if(isset($_SERVER['REDIRECT_MADOQUAHOST'])) {
        $_SERVER['MADOQUAHOST'] = $_SERVER['REDIRECT_MADOQUAHOST'];
        $_SERVER['REWRITEBASE'] = $_SERVER['REDIRECT_REWRITEBASE'];
    }

    if(defined('MADOQUA_ROOT')) {
        $rootDirectory = MADOQUA_ROOT . '/madoqua_private';
    }
    if (!isset($rootDirectory) || !file_exists($rootDirectory)) {
        throw new Exception('Root directory not set or directory doesn\'t exist on file system');
    }
    //root directory must be set/exist

    $applicationDirectory = $rootDirectory . DIRECTORY_SEPARATOR . 'application';
    //the application directory contains the MVC part of the application

    $libraryPath  = $rootDirectory .  DIRECTORY_SEPARATOR . 'library';

    set_include_path($libraryPath . PATH_SEPARATOR . get_include_path());
    //include path

    /** main application class */
    require_once 'Madoqua/Application.php';

    Madoqua_Application::setRootDirectory($rootDirectory);
    Madoqua_Application::setApplicationDirectory($applicationDirectory);
    //set the root/application directory

    $applicationConfig = Madoqua_Application::getConfiguration();
    //configuration

    if(!$applicationConfig) {
    	throw new Exception('Could not load configuration file. Have you created your madoqua.xml file?');
    }

    date_default_timezone_set($applicationConfig->datetime->timezone);
    //set timezone

    ini_set('mbstring.func_overload', $applicationConfig->mbstring);
    //use mbstring function overloading with the level defined in app. config

    if(!extension_loaded('mbstring')) {
        $applicationConfig->mbstring = 0;
    }
    //disable mb string if it is not installed

    if ($applicationConfig->mbstring != 0) {
        mb_internal_encoding("UTF-8");
        mb_http_output("UTF-8");
    }
    //unicode support if mbstring... kinda

    Madoqua_Application::startMvc();
    //go, go go! \ó/ ...

} catch (Exception $exception) {
    include(dirname(__FILE__) . '/layouts/scripts/error/error.phtml');
    //include html file with error handling
}

